//
//  WorkOrderTabBarController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOTabBarController.h"
#import "WOInfoViewController.h"
#import "WOHistoryViewController.h"
#import "WOTimeViewController.h"
#import "WOFilesViewController.h"
#import "WOChecklistViewController.h"
#import "SQLiteReader.h"

@implementation WOTabBarController

@synthesize workOrder;

#pragma mark Initialization Methods

 /* Get an instance of the class */
- (id) initWithWorkOrder:(WorkOrder*)aWorkOrder
{
	if (self = [super init])
	{
		self.workOrder = aWorkOrder;
		
		// Make the title clickable by creating a button and setting it as the title view.
		titleButton = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)] autorelease];
		[titleButton addTarget:self action:@selector(swapTitle:) forControlEvents:UIControlEventTouchUpInside];
		
		// Set font and color
		titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
		[titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[titleButton setTitle:workOrder.address.street forState:UIControlStateNormal];
		
		// Set the button as the navigation item's title view.
		self.navigationItem.titleView = titleButton;
		
		// Set the background to groupTableViewBackgroundColor so the animation looks good.
		self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
		
		// Add view controllers
		[self addViewControllers];
	}
	return self;
}

/* Animate the tab bar coming in from the bottom of the screen */
- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	//Animating the tab bar.
	[self appearingAnimation];
}

/* Animate the tab bar moving down to the bottom of the screen */
- (void) viewWillDisappear:(BOOL)animated
{	
	[super viewWillDisappear:animated];
	
	//Animating the tab bar.
	[self disappearingAnimation];
}

#pragma mark -

#pragma mark Other Methods

/* Toggle the title between address and work order ID */
- (void) swapTitle:(id)sender
{	
	if (titleButton.currentTitle == workOrder.address.street)
		[titleButton setTitle:[NSString stringWithFormat:@"%i", workOrder.workID] forState:UIControlStateNormal];
	else
		[titleButton setTitle:workOrder.address.street forState:UIControlStateNormal];
}

/* Adds the view controllers to the tab bar */
- (void) addViewControllers
{
	WOInfoViewController *infoVC = [[WOInfoViewController alloc] initWithWorkOrder:workOrder];
	WOHistoryViewController *historyVC = [[WOHistoryViewController alloc] initWithWorkOrder:workOrder];
	WOTimeViewController *timeVC = [[WOTimeViewController alloc] initWithWorkOrder:workOrder];
	WOFilesViewController *filesVC = [[WOFilesViewController alloc] initWithWorkOrder:workOrder];
	WOChecklistViewController *checklistVC = [[WOChecklistViewController alloc] initWithWorkOrder:workOrder];
	
	NSArray* controllers = [NSArray arrayWithObjects:infoVC, historyVC, timeVC, filesVC, checklistVC, nil];
	self.viewControllers = controllers;	
	self.selectedIndex = 0;
	
	[infoVC release];
	[historyVC release];
	[timeVC release];
	[filesVC release];
	[checklistVC release];
}

#pragma mark -

#pragma mark Animation Methods

/* Animation stopped so we reattach the tab bar to the tab bar controller's view */
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
	// Reattach the tab bar.
	[self.view addSubview:self.tabBar];
	
	// Adjust the position of the tab bar.
	CGRect frame = self.tabBar.frame;
	frame.origin.y = self.tabBar.superview.frame.size.height - frame.size.height;
	self.tabBar.frame = frame;
}

/* The tab bar will disappear */
- (void) disappearingAnimation
{
	// Temporarily add the tab bar to the navigation controller's view.
	[self.navigationController.view addSubview:self.tabBar];
	
	// Adjust the position of the tab bar.
	CGRect frame = self.tabBar.frame;
	frame.origin.y = self.tabBar.superview.frame.size.height - frame.size.height;
	self.tabBar.frame = frame;
	
	// Begin animations.
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:0.4];
	
	// Move the tab bar out of the screen.
	frame = self.tabBar.frame;
	frame.origin.y += frame.size.height;
	self.tabBar.frame = frame;
	
	[UIView commitAnimations];
}

/* The tab bar will appear */
- (void) appearingAnimation
{
	//Temporarily add the tab bar to the navigation controllers view.
	[self.navigationController.view addSubview:self.tabBar];
	
	//Put the tab bar just below the screen.
	CGRect frame = self.tabBar.frame;
	frame.origin.y = self.tabBar.superview.frame.size.height;
	self.tabBar.frame = frame;
	
	//Begin animations.
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:0.4];
	
	//Move the tab bar into view.
	frame = self.tabBar.frame;
	frame.origin.y -= frame.size.height;
	self.tabBar.frame = frame;
	
	[UIView commitAnimations];
}

#pragma mark -

@end